package gui.nodes;

import gui.AbstractViewPane;
import gui.GroupeUIConsulter;
import gui.MainController;
import gui.SemestreUIConsulter;
import gui.AbstractViewPane.ViewMode;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import utility.Record;

import business.AnneeEtudeBL;
import business.GroupeBL;
import business.SemestreBL;

public class SemestreRoot extends AbstractNode {
	
	int anneeEtudeId;
	
	public SemestreRoot(int aeId){
		super();
		this.anneeEtudeId = aeId;
	}

	public String getNodeName() {
		return "Semestres";
	}

	public void loadPrimaryViewPane() {

	}
	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new SemestreUIConsulter(this,AbstractViewPane.ViewMode.AJOUT, -1, anneeEtudeId);
		MainController.getInstance().setViewPane(newViewPane);
	}
	
	public JPopupMenu makePopup() {

		return new Popup();
	}
	
	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter un semestre");
		
		public Popup() {
			ajouter.addActionListener(this);

			add(ajouter);
		}
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter){
				loadAjouterViewPane();
			}
			
		}
		
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();

		AnneeEtudeBL aeBL = new AnneeEtudeBL(anneeEtudeId);
		
		for (Record recordSemestre : aeBL.getListeSemestre()) {
			SemestreNode semestreNode = new SemestreNode(this,recordSemestre, anneeEtudeId);
			add(semestreNode);
		}
	}

}
